package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.HdxywzxjlbBsDetailBo;
import com.ruoyi.report_ljlm.domain.vo.HdxywzxjlbBsDetailVo;
import com.ruoyi.report_ljlm.domain.HdxywzxjlbBsDetail;
import com.ruoyi.report_ljlm.mapper.HdxywzxjlbBsDetailMapper;
import com.ruoyi.report_ljlm.service.IHdxywzxjlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面厚度、芯样完整性试验检测记录(钻芯法)详情Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class HdxywzxjlbBsDetailServiceImpl implements IHdxywzxjlbBsDetailService {

    private final HdxywzxjlbBsDetailMapper baseMapper;

    /**
     * 查询路面厚度、芯样完整性试验检测记录(钻芯法)详情
     */
    @Override
    public HdxywzxjlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询路面厚度、芯样完整性试验检测记录(钻芯法)详情列表
     */
    @Override
    public TableDataInfo<HdxywzxjlbBsDetailVo> queryPageList(HdxywzxjlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HdxywzxjlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<HdxywzxjlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面厚度、芯样完整性试验检测记录(钻芯法)详情列表
     */
    @Override
    public List<HdxywzxjlbBsDetailVo> queryList(HdxywzxjlbBsDetailBo bo) {
        LambdaQueryWrapper<HdxywzxjlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HdxywzxjlbBsDetail> buildQueryWrapper(HdxywzxjlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HdxywzxjlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, HdxywzxjlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuangh()), HdxywzxjlbBsDetail::getZhuangh, bo.getZhuangh());
        lqw.eq(StringUtils.isNotBlank(bo.getChedwz()), HdxywzxjlbBsDetail::getChedwz, bo.getChedwz());
        lqw.eq(bo.getShiybh() != null, HdxywzxjlbBsDetail::getShiybh, bo.getShiybh());
        lqw.eq(bo.getQvyrq() != null, HdxywzxjlbBsDetail::getQvyrq, bo.getQvyrq());
        lqw.eq(bo.getQvyzhjwz() != null, HdxywzxjlbBsDetail::getQvyzhjwz, bo.getQvyzhjwz());
        lqw.eq(bo.getChangd1() != null, HdxywzxjlbBsDetail::getChangd1, bo.getChangd1());
        lqw.eq(bo.getChangd2() != null, HdxywzxjlbBsDetail::getChangd2, bo.getChangd2());
        lqw.eq(bo.getChangd3() != null, HdxywzxjlbBsDetail::getChangd3, bo.getChangd3());
        lqw.eq(bo.getChangd4() != null, HdxywzxjlbBsDetail::getChangd4, bo.getChangd4());
        lqw.eq(bo.getPingj() != null, HdxywzxjlbBsDetail::getPingj, bo.getPingj());
        lqw.eq(StringUtils.isNotBlank(bo.getXinyqkms()), HdxywzxjlbBsDetail::getXinyqkms, bo.getXinyqkms());
        return lqw;
    }

    /**
     * 新增路面厚度、芯样完整性试验检测记录(钻芯法)详情
     */
    @Override
    public Boolean insertByBo(HdxywzxjlbBsDetailBo bo) {
        HdxywzxjlbBsDetail add = BeanUtil.toBean(bo, HdxywzxjlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改路面厚度、芯样完整性试验检测记录(钻芯法)详情
     */
    @Override
    public Boolean updateByBo(HdxywzxjlbBsDetailBo bo) {
        HdxywzxjlbBsDetail update = BeanUtil.toBean(bo, HdxywzxjlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HdxywzxjlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面厚度、芯样完整性试验检测记录(钻芯法)详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
